package com.supostacks.infrastructure.repository;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supostacks.domain.repository.IFileManagementRepository;
import com.supostacks.domain.vo.FileManagementVo;
import com.supostacks.infrastructure.context.DefaultContext;
import com.supostacks.infrastructure.context.EntityUtil;
import com.supostacks.infrastructure.dao.FileManagementDao;
import com.supostacks.infrastructure.po.FileManagement;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class FileManagementRepository extends DefaultContext implements IFileManagementRepository {
    @Resource
    FileManagementDao fileManagementDao;

    @Override
    public String insert(FileManagementVo fileManagementVo) {
        try {
            FileManagement fileManagement = new FileManagement();
            EntityUtil.copyFields(fileManagementVo, fileManagement);
            int insert = fileManagementDao.insert(fileManagement);
            if (insert > 0) {
                return fileManagement.getId();
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String id) {
        fileManagementDao.deleteById(id);
    }

    @Override
    public String updateById(FileManagementVo fileManagementVo) {
        try {
            FileManagement fileManagement = new FileManagement();
            EntityUtil.copyFields(fileManagementVo, fileManagement);
            int i = fileManagementDao.updateById(fileManagement);
            if (i > 0) {
                return fileManagement.getId();
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public FileManagementVo selectById(String id) {
        try {
            FileManagement fileManagement = fileManagementDao.selectById(id);
            if (fileManagement != null) {
                FileManagementVo fileManagementVo = new FileManagementVo();
                EntityUtil.copyFields(fileManagement, FileManagementVo.class);
                return fileManagementVo;
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<FileManagementVo> selectList(JSONObject option) {
        QueryWrapper<FileManagement> queryWrapper = where(option);
        List<FileManagement> fileManagements = fileManagementDao.selectList(queryWrapper);
        return buildList(fileManagements);
    }

    @Override
    public JSONObject selectByPage(JSONObject options, int pageNo, int pageSize) {
        QueryWrapper<FileManagement> queryWrapper = where(options);

        Page<FileManagement> infos = new Page<>(pageNo, pageSize);
        IPage<FileManagement> iPage = fileManagementDao.selectPage(infos, queryWrapper);

        List<FileManagement> records = iPage.getRecords();

        JSONObject result = new JSONObject();
        result.put("total", records.size());
        result.put("list", records);

        return result;
    }


    public List<FileManagementVo> buildList(List<FileManagement> fileManagements) {
        return fileManagements.stream().map(fileManagement -> {
            try {
                FileManagementVo fileManagementVo = new FileManagementVo();
                EntityUtil.copyFields(fileManagement, fileManagementVo);
                return fileManagementVo;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
    }
}
